/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itsc.dsw2.vista;

//import java.awt.Desktop.Action;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.modelo.Categoria;

/**
 *
 * @author Pepe
 */
@ManagedBean
@SessionScoped
public class CategoryBean {
    @EJB
    private CategoriaBlLocal categoriaBl;
    private List<Categoria>lista;
    private Categoria category;
    private String accion;
    

    /**
     * Creates a new instance of CategoryBean
     */
    public CategoryBean() {
        category = new Categoria();
    }
    public List<Categoria> getLista() {
        lista=categoriaBl.getAll();
        return lista;
}

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }
    
    public String procesar(){
//        categoriaBl.Register(category);
//        return "ListaCategoria";
        if(accion.equalsIgnoreCase("Nuevo")){
            categoriaBl.Register(category);
        
        }else{
            if(accion.equalsIgnoreCase("Editar")){
                categoriaBl.modify(category);
                
        }else{
                if(accion.equalsIgnoreCase("Eliminar")){
                categoriaBl.delete(category);
            }
        }
        }
        accion="";
        return "ListaCategoria";
        
    
    
        
    }

//------------Listeners-----------------------
    public void prepararNuevo(ActionEvent evt) {
         accion="Nuevo";
       category = new Categoria();
    }
    public void prepararEditar(ActionEvent evt) {
        accion="Editar";
          int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
         category=lista.get(index);
     }
     public void prepararEliminar(ActionEvent evt) {
         accion="Eliminar";
         int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
         category=lista.get(index);
}
    
}